import {
  formatDate,formatDateReal
} from '../../utils/conmon'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
   
  

    countPic:9,//上传图片最大数量
    showImgUrl: "", //路径拼接，一般上传返回的都是文件名，
    uploadImgUrl:'',//图片的上传的路径
    isEdit:1,//1是可以提交  0 是查看详情
    // 老师发布作业介绍
    workInfo:{}, //作业信息
    stuInfo:{}, //学生信息
    imgDetailPics:[],//学生上传图片
    //学生提交数据
    pageData:{},
    headImg:'',
    stuWorkConten:{},//老师批阅详情
    isEdit:0,//是否批改 0 未批改 1已批改
    //选择学生
    middleShow:false,
    stuDatas:[],
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "作业详情"
    })
    this.setData({
      workInfo:JSON.parse(options.workInfo),
      stuInfo:JSON.parse(options.stuInfo),
      isEdit:options.isMark
    })
    this.setStuData()

    console.log(this.data.isEdit+'====接口数据a='+this.data.headImg);
    console.log('接口数据v='+JSON.stringify(this.data.stuInfo));

   
    
  },

  //设置头像跟提供作业图片
  setStuData(){
    if(this.data.stuInfo.headUrl!=null){
      this.setData({
        headImg:this.data.stuInfo.headUrl,
      }) 
    }else{
      var tHeadImge
      if(this.data.stuInfo.gender == 2){
        tHeadImge = "/images/tx.png"
      }else{
        tHeadImge = "/images/txn.png"
      }    
      this.setData({  
        headImg:tHeadImge
      })
    }

    if(this.data.stuInfo.pictureUrls!=null){
      console.log("======+str=="+JSON.stringify(this.data.stuInfo.pictureUrls.join(',')));
      var dataStr = this.data.stuInfo.pictureUrls.join(',');
      this.setData({
        imgDetailPics:dataStr.split(',')
      })
    }else{
      this.setData({
        imgDetailPics:[]
      })
    }
  },


  //头像加载失败
  imgerr(e){
    var tHeadImge
    if(this.data.stuInfo.gender == 2){
      tHeadImge = "/images/tx.png"
    }else{
      tHeadImge = "/images/txn.png"
    }    
    this.setData({  
      headImg:tHeadImge
    })
  },


  //编辑作业数据设置
  setPageData(){
   

  },

  //点击切换学生
  switchClick(){
    this.setData({
      middleShow:true
    })
    this.getHomeworkInfo()
  },
  

//获取作业提交相关接口
getHomeworkInfo(){
 
 wx.showLoading({
   title: '数据加载中...',
   icon: 'none',
 })

 var datas = {
   classId:this.data.workInfo.classId!=null?this.data.workInfo.classId:1,
   homeworkId:this.data.workInfo.homeworkId,
 }
 console.log('接口参数='+JSON.stringify(datas));
 app.api.getHomeworkInfo(datas).then(rest => {
   wx.hideLoading()
   console.log('接口数据a='+JSON.stringify(rest));
   if(rest&&rest.success){ 

     // var vTompletedNum = rest.data.completedList!=null?rest.data.completedList.length:0;
     // var vTotalNum = vTompletedNum + rest.data.undoneList!=null?rest.data.undoneList.length:0
     this.setData({
      stuDatas:rest.data.completedList,
       // totalNum:vTotalNum,
       // completedNum:vTompletedNum
     })

     
     console.log('接口数据abbb='+JSON.stringify(this.data.stuDatas));
   }else{
     console.log('接口数据=='+rest.errMsg)
     wx.showToast({
       title: rest.errMsg||'网络异常，请稍后重试！！',
       icon: 'none',
       duration: 2000,
       mask:true
     })
   }
 })
 .catch(r => {
   wx.hideLoading()
   console.error(r)
   wx.showToast({
     title: '网络异常，请稍后重试！',
     icon: 'none',
     duration: 2000,
     mask:true
   })
 })
},


  //点评
  submitData(){//提交数据
    //var data = this.data.dataDetail
    var homeworkId = this.data.workInfo.homeworkId
    var studentId = this.data.stuInfo.studentId
    wx.navigateTo({
      url: '../../pages/record/approvalWorksTeachDetail?homeworkId='+homeworkId+"&studentId="+studentId
   })
    
  },

  subMitFunction(){//提交函数
    var dataDetail = this.data.dataDetail
    dataDetail.schoolId =parseInt(this.data.schoolAreaKey)
    dataDetail.classType = parseInt(this.data.classTypesKey)
    dataDetail.nj = parseInt(this.data.gradeKey)
    dataDetail.classId = parseInt(this.data.classKey)
    var obj=dataDetail;
    for(var key in obj){
      if(obj[key] =='' && key!='loopRule'){
        delete obj[key]
      }
    }



  },


 //家长作业详情
 getData(){
 
  var data = {
    classId:this.data.workInfo.classId,
    homeworkId:this.data.workInfo.homeworkId,
    studentId:this.data.stuInfo.studentId
  }
  wx.showLoading({
    title: '数据加载中...',
    icon: 'none',
  })
  var that = this;
  console.log('家长作业详情接口参数='+JSON.stringify(data))
  app.api.getHomeWorkDetail(data).then(rest => {
    
    wx.hideLoading()
    console.log('家长作业详情接口数据='+JSON.stringify(rest))
    if(rest&&rest.success){   
      if(rest.data.list!=null && rest.data.list.length > 0){
        this.setData({
          stuWorkConten:rest.data.list[0],
          isEdit:1
        })

        // if(this.data.stuWorkConten.pictureUrl!=null){
        //   var dataStr = this.data.stuWorkConten.pictureUrl.join(',');
        //   this.setData({
        //     stuImgDetailPics:dataStr.split(',')
        //   })
        // }
        // console.log("=2====="+JSON.stringify(this.data.stuImgDetailPics))
      }
      console.log("=批改作业信息000====="+this.data.isEdit)
     
    }else{
      wx.showToast({
        title: rest.errMsg||'网络异常,请重试！',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    }
  })
  .catch(r => {
    console.error(r)

    wx.showToast({
      title: '查询失败,请重试！',
      icon: 'none',
      duration: 2000,
      mask:true
    })
  })
},


  editHomeWork(){//提交函数
    var dataDetail = this.data.dataDetail
    dataDetail.schoolId =parseInt(this.data.schoolAreaKey)
    dataDetail.classType = parseInt(this.data.classTypesKey)
    dataDetail.nj = parseInt(this.data.gradeKey)
    dataDetail.classId = parseInt(this.data.classKey)
    var obj=dataDetail;
    for(var key in obj){
      if(obj[key] ==''){
        delete obj[key]
      }
    }
  },
  

    //提测弹框消失回调
    hiddenAlert(){
      console.log("弹框异常了");
      this.setData({
        middleShow:false,
        bottom:false
      })
      this.drawPie()
      if(this.data.ifShowBar){
        this.drawBar()
      }
      // chart.setOption(this.setPieoption());
      // if(this.data.ifShowBar){
      //   barChart.setOption(this.setDrawoption());
      // }
    },

  // 学生选择-------------------------------------
  //弹框选择体测回调方法
  getCycleId(ret){
    console.log("弹框选择回调=="+ret.detail.val);
  
    this.setData({
      stuInfo:this.data.stuDatas[ret.detail.val],
      middleShow:false
    })
    this.setStuData()
    console.log('弹框选择回调nnnn='+JSON.stringify(this.data.stuInfo));
    //this.getPhysicalReportClass()
  },




hasEmoji (value) {
    return /[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF][\u200D|\uFE0F]|[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF]|[0-9|*|#]\uFE0F\u20E3|[0-9|#]\u20E3|[\u203C-\u3299]\uFE0F\u200D|[\u203C-\u3299]\uFE0F|[\u2122-\u2B55]|\u303D|[\A9|\AE]\u3030|\uA9|\uAE|\u3030/ig.test(value);
  },

    //监听组件事件，返回的结果
    //pictureUrl 作业图片
    myEventListener:function(e){
     
     },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // console.log("onShow=="+this.data.isEdit);
    //  //获取老师批改情况
    //  if(this.data.isEdit == 1){
    //   this.getData();
    // }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})